package com.project.erp.action.system;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.project.erp.action.BaseAction;
import com.project.erp.common.utils.Page;
import com.project.erp.model.system.Posts;
import com.project.erp.service.system.PostsService;

/**
 * 
 * COPYRIGHT (C) 2013 BY XXX公司

 *  功能模块名：系统管理-岗位管理

 *  功能描述：岗位管理action分发处理类

 * @author LiJiaGuo    

 * @version 1.0  

 * @created 2013-12-1 下午3:59:08
 */
@Controller
@Scope("prototype")
public class PostsAction extends BaseAction implements ModelDriven<Posts> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2490583926498710691L;
	private Posts posts;
	
	private String postsIds;
	
	@Autowired
	private PostsService postsService;
	
	/**
	 * 取得岗位列表
	 * @return
	 * @throws Exception
	 */
	public String getPostsListData() throws Exception{
		logger.debug("entry getPostsListData method ...");
		try{
			Page<Posts> page = new Page<Posts>();
			page.setPageNo(getPage());
			page.setPageSize(getRows());
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
			page = postsService.getPostsPageBySearch(page, posts);
			
			//设置表格返回数据
			this.getResultMap().put("total", page.getTotalCount());
			this.getResultMap().put("rows", page.getResult());
		}catch(Exception e){
			logger.error("execute getPostsListData error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 到添加岗位页面
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String toAddPosts() throws Exception{
		logger.debug("entry toAddPosts method!...");
		
		return SUCCESS;
	}
	
	/**
	 * 保存岗位信息
	 * @return SUCCESS
	 * @throws Exception 异常信息
	 */
	public String addPosts() throws Exception{
		logger.debug("entry addPosts method!...");

		try{
			postsService.savePosts(posts);
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.add"),this.getText("erp.system.posts.businame")});
			this.getResultMap().put("msg",msgStr);
		}catch(Exception e){
			//返回错误信息
			logger.error("添加岗位信息失败,失败原因:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.add"),this.getText("erp.system.posts.businame")});
			this.getResultMap().put("msg",msgStr);
		}
		return SUCCESS;
	}
	
	/**
	 * 到修改岗位页面
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String toEditPosts() throws Exception{
		logger.debug("entry toEditPosts method!...");
		try{
			logger.info("********************postsId="+posts.getId());
			Long postsId = posts.getId();
			posts = postsService.getPostsById(postsId);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("execute toEditPosts page error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 修改岗位信息
	 * @return
	 * @throws Exception
	 */
	public String editPosts() throws Exception{
		logger.debug("entry editPosts method!...");
		try{
			logger.info("posts="+posts);
			Posts persistentPosts = postsService.getPostsById(posts.getId());
			BeanUtils.copyProperties(posts, persistentPosts, new String[]{});
			
			//修改岗位信息
			postsService.editPosts(persistentPosts);
			
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.edit"),this.getText("erp.system.posts.businame")});
			this.getResultMap().put("msg", msgStr);
		}catch(Exception e){
			//返回错误信息
			logger.error("修改岗位信息失败,失败原因:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.edit"),this.getText("erp.system.posts.businame")});
			this.getResultMap().put("msg", msgStr);
		}
		return SUCCESS;
	}
	
	/**
	 * 删除岗位信息
	 * @return
	 * @throws Exception
	 */
	public String delPosts() throws Exception{
		logger.debug("entry delPosts method!...");
		try{
			logger.info("postsIds="+postsIds);
			String[] arrayIds = postsIds.split(",");
			postsService.delPosts(arrayIds);
			
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.erp.delete"),this.getText("erp.system.posts.businame")});
			this.getResultMap().put("msg",msgStr);
		}catch(Exception e){
			//返回错误信息
			e.printStackTrace();
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.erp.delete"),this.getText("erp.system.posts.businame")});
			this.getResultMap().put("msg",msgStr);
		}
		return SUCCESS;
	}
	
	public String checkPosts() throws Exception{
		logger.debug("entry checkPosts method!...");
		try{
			this.getResultMap().put("checkFlag",true);
			if(StringUtils.isNotBlank(posts.getPostNum())){
				Posts post = postsService.getPostsByPostNum(posts.getPostNum());
				if(null != post){
					this.getResultMap().put("checkFlag",false);
				}
			}
		}catch(Exception e){
			//返回错误信息
			logger.error("entry checkPosts method exception:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
		}
		this.getResultMap().put("result",SUCCESS);
		return SUCCESS;
	}

	@Override
	public Posts getModel() {
		if(null == posts){
			posts = new Posts();
		}
		return posts;
	}

	public Posts getPosts() {
		return posts;
	}

	public void setPosts(Posts posts) {
		this.posts = posts;
	}

	public String getPostsIds() {
		return postsIds;
	}

	public void setPostsIds(String postsIds) {
		this.postsIds = postsIds;
	}
}
